package com.css.devops.rest;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.css.devops.service.DevopsJob;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Job API Operation
 */
@Api(tags = "Devops：操作任务的API接口")
@RestController
@RequestMapping("${devops-context}/job")
public class DevopsJobResource {

	@Resource(name = "${devops.service.job}")
	private DevopsJob devopsJob;
	
	@ApiOperation(value = "创建任务")
	@PostMapping
	public ResponseEntity<String> ceateJob(@RequestBody com.css.devops.domain.Job job) {
		try {
			devopsJob.ceateJob(job);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("创建任务失败！");
		}
		return ResponseEntity.ok("创建任务成功！");
	}
	
	@ApiOperation(value = "修改任务")
	@PutMapping
	public ResponseEntity<String> updateJob(@RequestBody com.css.devops.domain.Job job) {
		try {
			devopsJob.updateJob(job);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("修改任务失败！");
		}
		return ResponseEntity.ok("修改任务成功！");
	}
	
	@ApiOperation(value = "根据任务名称获取任务详情")
	@GetMapping("{jobName}")
	public ResponseEntity<com.css.devops.domain.Job> getJob(@PathVariable String jobName) {
		com.css.devops.domain.Job job = null;
		try {
			job = devopsJob.getJob(jobName);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(job);
		}
		return ResponseEntity.ok(job);
	}
	
	@ApiOperation(value = "获取所有任务列表")
	@GetMapping("getJobList")
	public ResponseEntity<List<com.css.devops.domain.Job>> getJobList() {
		List<com.css.devops.domain.Job> jobs = null;
		try {
			jobs = devopsJob.getJobList();
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(jobs);
		}
		return ResponseEntity.ok(jobs);
	}
	
	@ApiOperation(value = "根据视图获取获取任务列表")
	@GetMapping("getJobListByView/{viewName}")
	public ResponseEntity<List<com.css.devops.domain.Job>> getJobListByView(@PathVariable String viewName) {
		List<com.css.devops.domain.Job> jobs = null;
		try {
			jobs = devopsJob.getJobListByView(viewName);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(jobs);
		}
		return ResponseEntity.ok(jobs);
	}
	
	@ApiOperation(value = "根据任务名称获取任务配置详情")
	@GetMapping("getJobConfig/{jobName}")
	public ResponseEntity<String> getJobConfig(@PathVariable String jobName) {
		String configXml = null;
		try {
			configXml = devopsJob.getJobConfig(jobName);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(configXml);
		}
		return ResponseEntity.ok(configXml);
	}
	
	@ApiOperation(value = "根据任务名称构建任务")
	@GetMapping("buildJob/{jobName}")
	public ResponseEntity<String> buildJob(@PathVariable String jobName) {
		try {
			devopsJob.buildJob(jobName);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("无参构建任务失败！");
		}
		return ResponseEntity.ok("无参构建任务成功！");
	}
	
	@ApiOperation(value = "根据任务名称构建任务")
	@PostMapping("buildParamJob/{jobName}")
	public ResponseEntity<String> buildParamJob(@PathVariable String jobName,@RequestBody Map<String,String> param) {
		try {
			devopsJob.buildParamJob(jobName, param);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("带参构建任务失败！");
		}
		return ResponseEntity.ok("带参构建任务成功！");
	}
	
	@ApiOperation(value = "停止任务构建")
	@GetMapping("stopLastJobBuild/{jobName}")
	public ResponseEntity<String> stopLastJobBuild(@PathVariable String jobName) {
		try {
			devopsJob.stopLastJobBuild(jobName);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("停止任务构建失败！");
		}
		return ResponseEntity.ok("停止任务构建成功！");
	}
	
	@ApiOperation(value = "删除任务构建")
	@DeleteMapping("deleteJob/{jobName}")
	public ResponseEntity<String> deleteJob(@PathVariable String jobName) {
		try {
			devopsJob.deleteJob(jobName);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("删除任务构建失败！");
		}
		return ResponseEntity.ok("删除任务构建成功！");
	}
	
	@ApiOperation(value = "禁用任务构建")
	@GetMapping("disableJob/{jobName}")
	public ResponseEntity<String> disableJob(@PathVariable String jobName) {
		try {
			devopsJob.disableJob(jobName);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("禁用任务构建失败！");
		}
		return ResponseEntity.ok("禁用任务构建成功！");
	}
	
	@ApiOperation(value = "启用任务构建")
	@GetMapping("enableJob/{jobName}")
	public ResponseEntity<String> enableJob(@PathVariable String jobName) {
		try {
			devopsJob.enableJob(jobName);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("启用任务构建失败！");
		}
		return ResponseEntity.ok("启用任务构建成功！");
	}
}
